package com.gzf.day35_encryptiondemo;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.MainThread;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    //    private String url = "http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=";
    //%E6%AF%9B%E6%B3%BD%E4%B8%9C = 毛泽东
    private String url = "https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E6%AF%9B%E6%B3%BD%E4%B8%9C&oq=%E6%AF%9B%E6%B3%BD%E4%B8%9C&rsv_pq=bad44bd800003a5f&rsv_t=0f42ynROkegsIM6Vjthr4mGwGiDh%2BuSw6lWPn249tym3ZlTHfXo5IZFtNK0&rqlang=cn&rsv_enter=0&prefixsug=%E6%AF%9B%E6%B3%BD%E4%B8%9C&rsp=0";
    private TextView mTextView;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mTextView.setText(new String(((byte[]) msg.obj)));
        }
    };
    private String path = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initTherad();
    }

    private void initTherad() {
        try {
            String encode = URLDecoder.decode("%E6%AF%9B%E6%B3%BD%E4%B8%9C", "utf-8");
            mTextView.setText(encode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is = null;
                ByteArrayOutputStream baos = null;
                try {
                    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
                    if (conn.getResponseCode() == 200) {
                        is = conn.getInputStream();
                        baos = new ByteArrayOutputStream();
                        byte[] buf = new byte[1024];
                        int len = 0;
                        while ((len = is.read(buf)) != -1) {
                            baos.write(buf, 0, len);
                        }
                        Message message = mHandler.obtainMessage();
                        message.obj = baos.toByteArray();
                        message.sendToTarget();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (baos != null) {
                        try {
                            baos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
//                .start();
    }

    private void initView() {
        mTextView = (TextView) findViewById(R.id.textview);
    }


}
